package com.mvp.shq.washer.ui.shop.mall;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.ProductAdapter;
import com.mvp.shq.washer.adapter.ShopBannerViewHolder;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.ShopBanner;
import com.mvp.shq.washer.entity.shop.ShopProductJump;
import com.mvp.shq.washer.entity.shop.result.ProductResult;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.ui.shop.productDetail.ProductDetailActivity;
import com.mvp.shq.washer.utils.DBUtil;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.ImageUtil;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.orhanobut.logger.Logger;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MallActivity extends BaseActivity<Presenter, Model> implements MallConstract.View {

    @BindView(R.id.banner)
    MZBannerView banner;
    @BindView(R.id.menu_item)
    FloatingActionButton menuItem;
    @BindView(R.id.menu_item1)
    FloatingActionButton menuItem1;
    @BindView(R.id.menu)
    FloatingActionMenu menu;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.menu_item2)
    FloatingActionButton menuItem2;
    @BindView(R.id.img_recommend_pic1)
    ImageView imgRecommendPic1;
    @BindView(R.id.tv_recommend_title1)
    TextView tvRecommendTitle1;
    @BindView(R.id.tv_recommend_content1)
    TextView tvRecommendContent1;
    @BindView(R.id.img_recommend_pic2)
    ImageView imgRecommendPic2;
    @BindView(R.id.tv_recommend_title2)
    TextView tvRecommendTitle2;
    @BindView(R.id.tv_recommend_content2)
    TextView tvRecommendContent2;
    @BindView(R.id.img_recommend_pic3)
    ImageView imgRecommendPic3;
    @BindView(R.id.tv_recommend_title3)
    TextView tvRecommendTitle3;
    @BindView(R.id.tv_recommend_content3)
    TextView tvRecommendContent3;
    private TextView[] tvRecommentTitles;
    private TextView[] tvRecommentContents;
    private ImageView[] imgRecommentPics;
    ImageUtil imageUtil = new ImageUtil(this);
    private ProductAdapter adapter;
    private List<Product> products,recommentProducts;
    private List<ShopBanner> banners;
    @Override
    public int getLayoutId() {
        return R.layout.activity_mall;
    }


    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("U智商城");
        tvRecommentTitles = new TextView[]{tvRecommendTitle1,tvRecommendTitle2,tvRecommendTitle3};
        tvRecommentContents = new TextView[]{tvRecommendContent1,tvRecommendContent2,tvRecommendContent3};
        imgRecommentPics = new ImageView[]{imgRecommendPic1,imgRecommendPic2,imgRecommendPic3};
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 2, ContextCompat.getColor(context, R.color.new_item));
        recyclerview.addItemDecoration(divider);
        presenter.getBanner();
        menu.setClosedOnTouchOutside(true);
    }


    @OnClick({R.id.menu_item, R.id.rl_recomment1,R.id.rl_recomment2,R.id.rl_recomment3,R.id.menu_item1, R.id.back, R.id.ll_commodity, R.id.ll_integral, R.id.ll_type, R.id.more_recommend, R.id.menu_item2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ll_commodity:
                //积分换购
                ShopProductJump params = new ShopProductJump();
                params.setType(ShopProductJump.PRODUCT_TYPE_INTEGRAL);
                params.setTitle("积分换购");
                JumpUtil.toProductList(context,params);
                break;
            case R.id.ll_integral:
                //全部商品
                ShopProductJump param = new ShopProductJump();
                param.setType(ShopProductJump.PRODUCT_TYPE_ALL);
                param.setTitle("全部商品");
                JumpUtil.toProductList(context,param);
                break;
            case R.id.ll_type:
                JumpUtil.toCategory(context);
                break;
            case R.id.more_recommend:
                ShopProductJump param1 = new ShopProductJump();
                param1.setType(ShopProductJump.PRODUCT_TYPE_ALL);
                param1.setTitle("全部商品");
                JumpUtil.toProductList(context,param1);
                break;
            case R.id.menu_item2:
                //个人中心
                JumpUtil.toShopUserCenter(context);
                menu.close(true);
                break;
            case R.id.menu_item:
                //首页
                menu.close(true);
                break;
            case R.id.menu_item1:
                //购物车
                JumpUtil.toShopCar(context);
                menu.close(true);
                break;
            case R.id.rl_recomment1:
                if (recommentProducts!=null && recommentProducts.size() > 0){
                    JumpUtil.toProductDetail(context,recommentProducts.get(0).getId(), ProductDetailActivity.PRODUCT_TYPE_SPU);
                }
                break;
            case R.id.rl_recomment2:
                if (recommentProducts!=null && recommentProducts.size() > 1){
                    JumpUtil.toProductDetail(context,recommentProducts.get(1).getId(), ProductDetailActivity.PRODUCT_TYPE_SPU);
                }
                break;
            case R.id.rl_recomment3:
                if (recommentProducts!=null && recommentProducts.size() > 2){
                    JumpUtil.toProductDetail(context,recommentProducts.get(2).getId(), ProductDetailActivity.PRODUCT_TYPE_SPU);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();
    }

    @Override
    public void getRecommentProductS(List<Product> list) {
        int MAX = list.size() > 3 ? 3 : list.size();
        recommentProducts = list;
        for (int i=0;i<MAX;i++){
            //tvRecommentTitles[i].setText(list.get(i).get);
            tvRecommentTitles[i].setText(list.get(i).getProductName());
            imageUtil.loadUrlImage(list.get(i).getImgUrl(),imgRecommentPics[i]);
        }
    }

    @Override
    public void getHotProductS(List<Product> list) {
        if (list == null || list.size() <= 0){
            return;
        }
        products = list;
        adapter = new ProductAdapter(context,list);
        adapter.setListener(new ProductAdapter.OnProductItemClickListener() {
            @Override
            public void onClick(int position) {
                String productId = products.get(position).getId();
                Logger.d(position + "商品ID:"+productId);
                JumpUtil.toProductDetail(context,productId, ProductDetailActivity.PRODUCT_TYPE_SKU);
            }
        });
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void getBannerS(List<ShopBanner> list) {
        banners = list;
        banner.setPages(banners, new MZHolderCreator<ShopBannerViewHolder>() {
            @Override
            public ShopBannerViewHolder createViewHolder() {
                return new ShopBannerViewHolder();
            }
        });
        banner.setIndicatorVisible(false);
        banner.setDelayedTime(3000);
        banner.setDuration(3000);
        banner.start();
    }

}
